package com.agil.photoalbum.swing.admin;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

import com.agil.photoalbum.db.dao.Functions;
import com.agil.photoalbum.db.entities.User;
import com.agil.photoalbum.db.util.SessionManager;

/**
 *
 * @author Hovanessyan
 */
public class UsersTableModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        
        private String[] columnNames = { "uid", "username", "First Name", "Last Name", "E-mail" };
        
        private ArrayList<Object[]> tableData = new ArrayList<Object[]>();

        public UsersTableModel() {
            SessionManager.openSession();
            for (User u : Functions.getAllUsers()) {
                tableData.add(new Object[]{u.getUserId(), u.getUserName(), u.getFName(), u.getLName(), u.getEmail()});
            }
            SessionManager.closeSession();
        }

        public int getColumnCount() {
            return columnNames.length;
        }

    @Override
        public String getColumnName(int col) {
            return columnNames[col];
        }
 
        public int getRowCount() {
            return tableData.size();
        }
        

        public Object getValueAt(int rowIndex, int colIndex) {
            if (tableData.get(rowIndex) != null && tableData.get(rowIndex)[colIndex] != null) {
                return tableData.get(rowIndex)[colIndex];
            }
        	else{ return "";}
        }

  
    @Override
        public void setValueAt(Object value, int row, int col) {
            tableData.get(row)[col] = value;
            fireTableCellUpdated(row, col);
        }
    
    
    boolean[] canEdit = new boolean[] { false, false, false, false, false };
  
    @Override
        public boolean isCellEditable(int row, int col) {
            return canEdit[col];
        }

        public void addNewRow(Object[] newRow) {
            this.tableData.add(newRow);
        }

        public void deleteRow(int rowIndex) {
            this.tableData.remove(rowIndex);
        }
    }